package system

import (
	"context"
	v1 "gf-vben-admin/api/system/v1"
	model "gf-vben-admin/internal/model/system"
	service "gf-vben-admin/internal/service/system"
	"github.com/gogf/gf/v2/util/gconv"
)

var (
	Role = cRole{}
)

type cRole struct{}

//PageList 分页查询角色
func (c *cRole) PageList(ctx context.Context, req *v1.RolePageListReq) (res *v1.RolePageListRes, err error) {
	total, list, err := service.Role().PageList(ctx, req.Role)
	if err != nil {
		return nil, err
	}
	res = &v1.RolePageListRes{
		Total: total,
		Rows:  list,
	}
	return
}

// List 查询角色列表
func (c *cRole) List(ctx context.Context, req *v1.RoleListReq) (res *v1.RoleListRes, err error) {
	list, err := service.Role().List(ctx, req.Role)
	if err != nil {
		return nil, err
	}
	res = &v1.RoleListRes{
		Rows: list,
	}
	return
}

// Get 查询角色表详情
func (c *cRole) Get(ctx context.Context, req *v1.RoleGetReq) (res *v1.RoleGetRes, err error) {
	entity, err := service.Role().Get(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	res = &v1.RoleGetRes{
		Role: entity,
	}
	return
}

// Add 添加角色
func (c *cRole) Add(ctx context.Context, req *v1.RoleAddReq) (res *v1.RoleAddRes, err error) {
	in := new(model.Role)
	err = gconv.Struct(req, in)
	if err != nil {
		return nil, err
	}
	err = service.Role().Add(ctx, *in)
	return
}

// Edit 编辑角色
func (c *cRole) Edit(ctx context.Context, req *v1.RoleEditReq) (res *v1.RoleEditRes, err error) {
	in := new(model.Role)
	err = gconv.Struct(req, in)
	if err != nil {
		return nil, err
	}
	err = service.Role().Edit(ctx, *in)
	return
}

// EditState 编辑角色状态
func (c *cRole) EditState(ctx context.Context, req *v1.RoleEditStateReq) (res *v1.RoleEditRes, err error) {
	err = service.Role().EditState(ctx, req.Ids, req.State)
	return
}

// Delete 删除角色
func (c *cRole) Delete(ctx context.Context, req *v1.RoleDelReq) (res *v1.RoleDelRes, err error) {
	err = service.Role().Delete(ctx, req.Ids)
	return
}

// RoleScope 保存角色权限
func (c *cRole) RoleScope(ctx context.Context, req *v1.RoleScopeReq) (res *v1.RoleScopeRes, err error) {
	err = service.Role().RoleScope(ctx, req.RoleScope)
	return
}
